/*
 * Copyright (c) 2024-2025 林羽 (admin@eova.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import XeTool from 'xe-utils'

import { StringTool } from './StringTool'
import { JsonTool } from './JsonTool'
import { TimeTool } from './TimeTool'
import { CookieTool } from './CookieTool'
import { UrlTool } from './UrlUtil'
import { DomTool } from './DomTool'
import { AxiosTool } from './AxiosTool'
import { ValidateTool } from './ValidateTool'

// 依赖类型导出
export * from './ValidateTool'

/**
 * 神器 · 疾风之刃
 * 
 * 开发者有三件必经之事：简单、复杂、然后简单
 * KISS原则:用户体验的高层境界，就是要把一个东西做得连小白都会用，因而也被称为<<懒人原则>>。
 * 
 * What：模块化的工具箱,物以类聚,依赖集中
 * Where：x.xxx.xxx简短快捷的输入操作,让你众享丝滑欲罢不能
 * Why：整合常用高频工具类,编码速度+50%,代码量-30%
 * 
 *
 * 为天地立心，为生民立命，
 * 为往圣继绝学，为万世开太平。
 *
 */
export class EovaTools {
  public str: StringTool
  public json: JsonTool
  public url: UrlTool
  public time: TimeTool
  public cookie: CookieTool
  public dom: DomTool
  public axios: AxiosTool
  public validate: ValidateTool

  // 第三方工具库, 丰富工具库能力
  public xe = XeTool // XEUtilsMethods

  constructor() {
    this.str = new StringTool()
    this.json = new JsonTool()
    this.url = new UrlTool()
    this.time = new TimeTool()
    this.cookie = new CookieTool()
    this.dom = new DomTool()
    this.axios = new AxiosTool()
    this.validate = new ValidateTool()
  }

  log = (s: any) => {
    console.log(s)
  }

  isEmpty = (o: any) => {
    if (
      o == undefined ||
      o == null ||
      o == 'undefined' ||
      o == 'null' ||
      o == 'NULL'
    ) {
      return true
    }

    // 检查字符串是否为空或仅包含空白字符
    if (typeof o === 'string' || o instanceof String) {
      return o.toString().trim() === ''
    }

    // 检查数组是否为空
    if (Array.isArray(o)) {
      return o.length === 0
    }

    // 检查Map是否为空
    if (o instanceof Map) {
      return o.size === 0
    }

    // 检查Set是否为空
    if (o instanceof Set) {
      return o.size === 0
    }

    // 如果不是以上类型，则认为不是空的
    return false
  }
}

// 创建一个全局的单例实例
export const x = new EovaTools()

export interface EovaExport {
  /**
   * 全局工具箱
   */
  x: EovaTools
}

/**
 * umd 引用时 window.eova = EovaTools, 所以会导致 import x form 报错 必须 import {x} ==> eova.x
 */
export default x

export const eova: EovaExport = { x }

// 声明全局变量
declare global {
  interface Window {
    eova: EovaExport
  }
}

// 将实例挂载到全局对象（UMD 环境）
if (typeof window !== 'undefined') {
  // 存在直接使用, 不存在重新赋对象
  window.eova = window.eova || eova
}
